package com.enjoyread.api.controller;

import com.enjoyread.common.bo.UserBo;
import com.enjoyread.common.bo.UserCodeBo;
import com.enjoyread.common.config.SystemConfig;
import com.enjoyread.common.vo.R;
import com.enjoyread.core.service.intf.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: JC
 * @time: 2021/10/18 17:27
 * @description:
 */
@RestController
@CrossOrigin//实现跨域
@RequestMapping("/api/auth/")
@Api(value = "认证模块接口",tags = "认证模块接口")
public class UserController {
    @Autowired
    private UserService service;

    //注册
    @ApiOperation(value = "实现账号注册",notes = "实现账号注册")
    @PostMapping("register")
    public R add(@RequestBody UserBo bo, HttpServletRequest request){
        return service.register(bo,request.getRemoteAddr());
    }
    //校验手机号是否被注册
    @ApiOperation(value = "校验手机号是否存在",notes = "校验手机号是否存在")
    @GetMapping("checkphone")
    public R checkPhone(String phone){
        return service.checkPhone(phone);
    }
    //手机号密码登录
    @ApiOperation(value = "实现手机号密码登录",notes = "实现手机号密码登录")
    @PostMapping("login")
    public R login(@RequestBody UserBo bo ,HttpServletRequest request){
        return service.loginPassword(bo,request.getRemoteAddr());
    }
    //手机号验证码登录
    @ApiOperation(value = "实现手机号验证码登录",notes = "实现手机号验证码登录")
    @PostMapping("logincode")
    public R logincode(@RequestBody UserCodeBo bo , HttpServletRequest request){
        return service.loginCode(bo,request.getRemoteAddr());
    }
    //找回密码--验证码
    @ApiOperation(value = "验证找回密码的验证码",notes = "验证找回密码的验证码")
    @PostMapping("findcode")
    public R findCode(@RequestBody UserCodeBo bo,HttpServletRequest request){
        return service.findPass(bo,request.getRemoteAddr());
    }
    //找回密码--改密码
    @ApiOperation(value = "找回密码进行密码的修改",notes = "找回密码进行密码的修改")
    @PostMapping("findupdatepass")
    public R findPass(@RequestBody UserBo bo,String ip){
        return service.updatePass(bo,ip);
    }
    //校验token
    @ApiOperation(value = "校验令牌",notes = "校验令牌")
    @GetMapping("checktoken")
    public R checkToken(HttpServletRequest request){
        return service.checkToken(request.getHeader(SystemConfig.HEADER_TOKEN));
    }
    //通过令牌获取用户信息
    @ApiOperation(value = "通过令牌获取用户信息",notes = "通过令牌获取用户信息")
    @GetMapping("getuserinfo")
    public R info(HttpServletRequest request){
        return service.queryUserInfo(request.getHeader(SystemConfig.HEADER_TOKEN));
    }
    //注销，退出登录
    @ApiOperation(value = "注销登录，退出",notes = "注销登录，退出")
    @GetMapping("loginout")
    public R loginOut(HttpServletRequest request){
        return service.loginOut(request.getHeader(SystemConfig.HEADER_TOKEN));
    }
}
